#include "linkedlist.h"


int init_array(int rv[])
{
	//rv = (int*)malloc(sizeof(int));
	//if(rv == NULL) return 0;

	adccount = 0;

	calType = typeNum;
	calNum = sampNum;

	bin_black = 0;
	bin_white = 0;
	bin_steel = 0;
	bin_alumn = 0;
	bin_dunno = 0;

	//adcSize = 1001;
	int r;
	for(r = 0; r < adcSize; r++) rv[r] = 4095;

	return 1;

}

void push_array(int input, int rv[])
{
	rv[adccount] = input;
	//rv[adccount+1] = 4095;
	adccount++;
	adccount%=adcSize;
}

int avg_array(int rv[])
{
	int a;
	int avg;
	avg = 4095;
	for(a = 1; a < (adcSize-1); a++)
	{
		avg = (rv[a] + rv[a-1])/2;
	}
	int r;
	for(r = 0; r < adcSize; r++) rv[r] = 0;

	return avg;
}

int peak_array(int rv[], int print)
{
	int a;
	int low;
	int hist1=4095;
	int hist2=4095;
	int hist3=4095;
	low = 4095;
	for(a = 0; a < (adcSize-1); a++)
	{
		if((rv[a] != 0) && (rv[a] < low))
		{
			hist3=hist2;
			hist2=hist1;
			hist1=low;
			low = rv[a];
		}
	}

	if(print) print_peak(low, 0);

	for(a = 0; a < adcSize; a++) rv[a] = 0;
	//optional
	low+=hist1;
	low+=hist2;
	low+=hist3;
	low=low/4;

	return low;
}

void print_peak(int low, int say)
{
	if((low < wphi) && (low > wplo))
	{
		pushQueue(whiteplastic);
		bin_white++;
		if(say) SYN_Say((unsigned char *)Volume"msgd 白色塑料");
		bt_UARTsend("White Plastic: ");
		bt_UARTsend(itoa(low,10));
		bt_UARTsend("\r\n\r\n");
	}
	else if((low < bphi) && (low > bplo))
	{
		pushQueue(blackplastic);
		bin_black++;
		if(say) SYN_Say((unsigned char *)Volume"msgd 黑色塑料");
		bt_UARTsend("Black Plastic: ");
		bt_UARTsend(itoa(low,10));
		bt_UARTsend("\r\n\r\n");
	}
	else if((low < shi) && (low > slo))
	{
		pushQueue(steel);
		bin_steel++;
		if(say) SYN_Say((unsigned char *)Volume"msgd 钢");
		bt_UARTsend("Steel: ");
		bt_UARTsend(itoa(low,10));
		bt_UARTsend("\r\n\r\n");
	}
	else if((low < alhi) && (low > allo))
	{
		pushQueue(aluminum);
		bin_alumn++;
		if(say) SYN_Say((unsigned char *)Volume"msgd 铝");
		bt_UARTsend("Aluminum: ");
		bt_UARTsend(itoa(low,10));
		bt_UARTsend("\r\n\r\n");
	}
	else
	{
		bin_dunno++;
		pushQueue(home);
		//bin_unkwn++;
		if(say) SYN_Say((unsigned char *)Volume"msga");
		bt_UARTsend("Dead Zone: ");
		bt_UARTsend(itoa(low,10));
		bt_UARTsend("\r\n\r\n");
	}
	//printQueueSize();
	//printllQueue(&head,&tail);
}

int calibrate(int rv[], int print)
{
	int low;
	low = 4095;

	low = peak_array(rv, print);
	bt_UARTsend(itoa(low,10));
	bt_UARTsend("\r\n");

	return low;
}

void dead_zone_guess(int low)
{
#define diff(x,y)(x-y)
#define min(x,y)(x<y?x:y)
}

int calRng(void)
{
	int i,j;
	int low[4], high[4];

	low[0] = 4095;
	low[1] = 4095;
	low[2] = 4095;
	low[3] = 4095;

	high[0] = 0;
	high[1] = 0;
	high[2] = 0;
	high[3] = 0;

	for(i = 0; i < typeNum; i++)
	{
		for(j = 0; j < sampNum; j++)
		{
			if(calSet[i][j] < low[i]) low[i] = calSet[i][j];
			if(calSet[i][j] > high[i]) high[i] = calSet[i][j];
		}
		/*	switch(i)
		{
		case 0: pushQueue(2); break;
		case 1: pushQueue(3); break;
		case 2: pushQueue(1); break;
		case 3: pushQueue(4); break;
		}
		 */
	}

	bmax = high[3];
	bmin = low[3];
	bt_UARTsend("Black ");
	bt_UARTsend(itoa(bmax,10));
	bt_UARTsend(" : ");
	bt_UARTsend(itoa(bmin,10));
	bt_UARTsend("\r\n");


	wmax = high[2];
	wmin = low[2];
	bt_UARTsend("White ");
	bt_UARTsend(itoa(wmax,10));
	bt_UARTsend(" : ");
	bt_UARTsend(itoa(wmin,10));
	bt_UARTsend("\r\n");


	smax = high[1];
	smin = low[1];
	bt_UARTsend("Steel ");
	bt_UARTsend(itoa(smax,10));
	bt_UARTsend(" : ");
	bt_UARTsend(itoa(smin,10));
	bt_UARTsend("\r\n");


	amax = high[0];
	amin = low[0];
	bt_UARTsend("Alumm ");
	bt_UARTsend(itoa(amax,10));
	bt_UARTsend(" : ");
	bt_UARTsend(itoa(amin,10));
	bt_UARTsend("\r\n");

	if (0)
	{
		int temp[4];

		temp[4]=(low[3]-high[2]) >> 2;
		wmax=wmax+temp[4];
		bmin=wmax+1;

		temp[3]=(low[2]-high[1]) >> 2;
		smax=smax+temp[3];
		wmin=smax+1;

		temp[2]=(low[1]-high[0]) >> 2;
		amax=amax+temp[2];
		smin=amax+1;

		bt_UARTsend("Black ");
		bt_UARTsend(itoa(bmax,10));
		bt_UARTsend(" : ");
		bt_UARTsend(itoa(bmin,10));
		bt_UARTsend("\r\n");

		bt_UARTsend("White ");
		bt_UARTsend(itoa(wmax,10));
		bt_UARTsend(" : ");
		bt_UARTsend(itoa(wmin,10));
		bt_UARTsend("\r\n");

		bt_UARTsend("Steel ");
		bt_UARTsend(itoa(smax,10));
		bt_UARTsend(" : ");
		bt_UARTsend(itoa(smin,10));
		bt_UARTsend("\r\n");
		bt_UARTsend("Alumm ");

		bt_UARTsend(itoa(amax,10));
		bt_UARTsend(" : ");
		bt_UARTsend(itoa(amin,10));
		bt_UARTsend("\r\n");

	}
	calibrateState = 0;


	return 0;
}
